import React from "react";

import BaseExecuter from "../common/BaseExecuter";
import Constants from "../../common/Constants";
import ResponseConstants from "../../common/ResponseConstants";

import DataManager from "../../datas/DataManager";

export default class LoginExecuter extends BaseExecuter {
  constructor() {
    super();

    this.tempUserId = -1;
  }
  execute(action) {
    switch (action.name) {
      case "LoginSendAction":
        this._sendLogin(action);
        break;
      case "RequestSmsAction":
        this._sendRequestSms(action);
        break;
      case "LoginOutAction":
        this._dealLogout(action);
        break;
      case "LoginCheckIsUserAction":
        this._checkIsUser(action);
        break;
      case "LoginSendOutAction":
        this._sendOut(action);
        break;
      case "LoginSendRegisteAction":
        this._sendRegiste(action);
        break;
      case "LoginSendVerifyAction":
        this._sendVerify(action);
        break;
      case "LoginCheckVersionAction":
        this._checkVersion(action);
        break;
      default:
    }
  }

  _checkVersion(action) {
    this._sendRequestCommonResponse(action, "checkVersion");
  }

  _sendVerify(action) {
    var actionData = action.data;
    DataManager.callMethod(Constants.MODULE_TYPE.LOGIN, "sendVerify", [
      actionData,
      (data, isSucceed, response) => {
        action.finish(data, isSucceed, response, () =>
          this._sendVerifyActionCallback(actionData)
        );
      }
    ]);
  }

  _sendVerifyActionCallback(actionData) {
    DataManager.callMethod(Constants.MODULE_TYPE.USER, "updateUserData", [
      actionData
    ]);
  }

  _sendRegiste(action) {
    var data = action.data;
    DataManager.callMethod(Constants.MODULE_TYPE.LOGIN, "sendRegiste", [
      data,
      (data, isSucceed, response) => {
        this._dealLoginSuccess(
          data,
          isSucceed,
          response,
          action,
          Constants.LOGIN_TYPE.USER
        );
      }
    ]);
  }

  // _dealRegisterSuccess(data, isSucceed, response, action) {
  //   if(isSucceed) {
  //     var authorization = "0";
  //     if(response && response.headers.map.authorization) {
  //       authorization = response.headers.map.authorization[0];
  //     }
  //     if(response.status == ResponseConstants.status_request_success ||
  //         response.status == ResponseConstants.status_created_success) {
  //       DataManager.callMethod(Constants.MODULE_TYPE.LOGIN,
  //         "updateAuthorization", [authorization]);
  //     }
  //   }
  //   action.finish(data, isSucceed, response);
  // }

  //主动退出
  _sendOut(action) {
    action.finish();
  }

  //检测是否是员工
  _checkIsUser(action) {
    var result = DataManager.callMethod(
      Constants.MODULE_TYPE.LOGIN,
      "checkIsUser"
    );
    action.finish(action.data.content, result);
  }

  //登出
  _dealLogout(action) {
    DataManager.callMethod(Constants.MODULE_TYPE.LOGIN, "clearLoginData");
    DataManager.callMethod(Constants.MODULE_TYPE.USER, "clearUserData");
    DataManager.callMethod(Constants.MODULE_TYPE.IM, "logout");
    action.finish();
  }

  _dealLoginSuccess(data, isSucceed, response, action, loginType) {
    if (isSucceed) {
      var authorization = "0";
      if (response && response.headers.map.authorization) {
        authorization = response.headers.map.authorization[0];
      }
      if (
        response.status == ResponseConstants.status_request_success ||
        response.status == ResponseConstants.status_deal_success ||
        response.status == ResponseConstants.status_created_success
      ) {
        DataManager.callMethod(Constants.MODULE_TYPE.USER, "setUserData", [
          data
        ]);
        DataManager.callMethod(
          Constants.MODULE_TYPE.LOGIN,
          "updateAuthorization",
          [authorization]
        );
        DataManager.callMethod(Constants.MODULE_TYPE.LOGIN, "updateLoginType", [
          loginType
        ]);
        DataManager.callMethod(Constants.MODULE_TYPE.IM, "login", [
          data.user_id,
          data.im_passwd
        ]);
      }
    }
    action.finish(data, isSucceed, response);
  }

  _sendLogin(action) {
    var actionData = action.data;
    if (actionData.type == "visitor") {
      DataManager.callMethod(Constants.MODULE_TYPE.LOGIN, "sendVisitorLogin", [
        (data, isSucceed, response) => {
          this._dealLoginSuccess(
            data,
            isSucceed,
            response,
            action,
            actionData.type
          );
        }
      ]);
    } else {
      this.tempUserId = actionData.userNo;
      DataManager.callMethod(Constants.MODULE_TYPE.LOGIN, "sendLogin", [
        actionData,
        (data, response, isSucceed) => {
          this._dealLoginSuccess(
            data,
            response,
            isSucceed,
            action,
            actionData.type
          );
        }
      ]);
    }
  }

  _sendRequestSms(action) {
    var data = action.data;
    DataManager.callMethod(Constants.MODULE_TYPE.LOGIN, "sendRequestSms", [
      data,
      (data, isSucceed, response) => {
        action.finish(data, isSucceed, response);
      }
    ]);
  }

  _getModuleType() {
    return Constants.MODULE_TYPE.LOGIN;
  }
}
